package com.lenovo.manufacture.net;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.beardedhen.androidbootstrap.BootstrapText;
import com.beardedhen.androidbootstrap.TypefaceProvider;
import com.lenovo.manufacture.R;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaseActivity extends AppCompatActivity {
    private static final String TAG = "BaseActivity";
    protected static final PostRequest data = new Retrofit.Builder().baseUrl(PostRequest.url).addConverterFactory(GsonConverterFactory.create()).build().create(PostRequest.class);
    protected static Map<String, Object> json = new HashMap<>();
    protected test  t= new test();
    protected Toast toast;
    public ProgressDialog dialog;
    public DecimalFormat decimalFormat = new DecimalFormat("#,###");
    public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypefaceProvider.registerDefaultIconSets();
        dialog = new ProgressDialog(this);
        dialog.setMessage("Dialog...");
    }

    protected final <T> void async(Call<T> call, Callable<T> callable) {
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                Log.i(TAG, "onResponse: " + response.toString());
                if (response.isSuccessful()) {
                    Log.i(TAG, "onResponse: " + response.body().toString());
                    callable.call(response.body());
                }
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                Toast.makeText(BaseActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                t.printStackTrace();
            }
        });
    }

    protected interface Callable<T> {
        void call(T v);
    }

    protected void sort(List<Map<String, String>> maps, String v, BootstrapLabel tv, String s) {
        boolean b = tv.getTag() != null && ((boolean) tv.getTag());
        Collections.sort(maps, (o1, o2) -> {
            if (o1.get(v).matches("\\d+"))
                return b ? Integer.valueOf(o1.get(v)) - Integer.valueOf(o2.get(v)) : Integer.valueOf(o2.get(v)) - Integer.valueOf(o1.get(v));
            return b ? o1.get(v).compareTo(o2.get(v)) : o2.get(v).compareTo(o1.get(v));
        });
        String id = b ? "md_arrow_drop_down" : "md_arrow_drop_up";
        tv.setBootstrapText(new BootstrapText.Builder(this).addText(s).addMaterialIcon(id).build());
        tv.setTag(!b);
    }

    public class test{
        public test test(String k,Object v) {
            json.put(k,v);
            return this;
        }
        public Map<String,Object> b(){
            return json;
        }
    }
    protected void showMsg(String msg) {
        if (toast != null)
            toast.cancel();
        toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
        toast.show();
    }
}
